/**************************************************************
 *@ProjectName: blweibo
 *@FileName: EditActivity.java
 *@Date: 2011-5-31
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.Editable;
import android.text.Selection;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhiweibo.models.Topic;
import com.zhiweibo.util.Constant;
import com.zhiweibo.util.Helper;
import com.zhiweibo.util.HttpUtil;
import com.zhiweibo.util.Logger;
import com.zhiweibo.util.TopicUtil;

/**
 * The Class EditActivity.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-31
 */
public class EditActivity extends BaseActivity implements TextWatcher, OnClickListener {

    /** The m et. */
    private EditText mEt;

    /** The topic. */
    private Topic topic;

    /** The edit task. */
    private EditTask editTask;

    /** The ib insert pic. */
    private ImageButton ibInsertPic;

    /** The ib insert topic. */
    private ImageButton ibInsertTopic;

    /** The ib insert at. */
    private ImageButton ibInsertAt;

    /** The ib insert face. */
    private ImageButton ibInsertFace;

    /** The insert pic view. */
    private ImageView insertPicView;

    /** The sdcard temp file. */
    private File sdcardTempFile;

    /** The m emotion view. */
    private EmotionView mEmotionView;

    /** The m emotion adapter. */
    private final EmotionView.EmotionAdapter mEmotionAdapter;

    /** The m input method manager. */
    private InputMethodManager mInputMethodManager;

    /** The bitmap. */
    private Bitmap bitmap;
    // 0 new 1 comment 2 forward 3 message 4 feedback 5 reply_comment 6
    // replay_message
    /** The mode. */
    private int mode;

    /** The str topic id. */
    private String strTopicId;

    /** The local uri. */
    private Uri localUri;

    /** The str content. */
    private String strContent;

    /** The uid. */
    private String uid;

    /** The cid. */
    private String cid;

    /** The tv_text_limit. */
    private TextView tv_text_limit;

    /** The ll_operation_thesame. */
    private LinearLayout ll_operation_thesame;

    /** The llimte. */
    private View llimte;

    /** The rb_comment_sametime. */
    private CheckBox rb_comment_sametime;

    /** The m face click healper. */
    private FaceClickHealper mFaceClickHealper;

    /** The tv_extra_feature_desc. */
    private TextView tv_extra_feature_desc;

    /** The allword. */
    private int allword = 140;

    /** The pic. */
    private File fPic;

    /**
     * Instantiates a new edits the activity.
     */
    public EditActivity() {
        mEmotionAdapter = new EmotionView.EmotionAdapter() {
            @Override
            public void doAction(int paramInt, String paramString) {
                int i = mEt.getSelectionStart();
                int j = mEt.getSelectionEnd();
                String str1 = mEt.getText().toString();
                String str2 = "[" + paramString + "]";
                SpannableStringBuilder localSpannableStringBuilder = new SpannableStringBuilder();
                localSpannableStringBuilder.append(str1, 0, i);
                localSpannableStringBuilder.append(str2);
                localSpannableStringBuilder.append(str1, j, str1.length());
                TopicUtil.highlightContent(EditActivity.this, localSpannableStringBuilder, false);
                mEt.setText(localSpannableStringBuilder, TextView.BufferType.SPANNABLE);
                int m = str2.length() + i;
                mEt.setSelection(m);
            }
        };
        File localFile1 = Environment.getExternalStorageDirectory();
        File path = new File(localFile1.getPath() + File.separator + "zhiweibo/weibo");
        if (!path.exists()) {
            path.mkdirs();
        }
        fPic = new File(path, "praImage");
        if (!fPic.exists()) {
            try {
                fPic.createNewFile();
            } catch (IOException e) {
                Logger.d(e);
            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yingnet.zhiweibo.BaseActivity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        Display localDisplay = getWindowManager().getDefaultDisplay();
        int i = localDisplay.getWidth();
        int j = localDisplay.getHeight();
        WeiboApplication.setDisplay(i, j);
        mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        mode = getIntent().getIntExtra("mode", 0);
        strTopicId = getIntent().getStringExtra("topicId");
        strContent = getIntent().getStringExtra("content");
        uid = getIntent().getStringExtra("uid");
        cid = getIntent().getStringExtra("cid");
        setView(R.layout.newblog2);
        String str1 = null;
        switch (mode) {
        case 0:
            str1 = getString(R.string.title_new_mblog);
            break;
        case 1:
            str1 = getString(R.string.title_comment);
            break;
        case 2:
            str1 = getString(R.string.title_forward);
            break;
        case 3:
            str1 = getString(R.string.main_message);
            break;
        case 4:
            str1 = getString(R.string.weibo_feedback);
            break;
        case 5:
            str1 = getString(R.string.itemmenu_reply_comment);
            break;
        case 6:
            str1 = getString(R.string.itemmenu_reply_message);
            break;
        }
        setTitleBar(1, getString(R.string.imageviewer_back), str1, getString(R.string.title_button_send));
        mFaceClickHealper = new FaceClickHealper(this);
        mEmotionView = (EmotionView) findViewById(R.id.emotion_view);
        mEmotionView.setEmotionAdapter(mEmotionAdapter);
        mEt = (EditText) findViewById(R.id.et_mblog);
        mEt.addTextChangedListener(this);
        mEt.setOnClickListener(this);
        llimte = findViewById(R.id.ll_text_limit_unit);
        tv_text_limit = (TextView) findViewById(R.id.tv_text_limit);
        llimte.setOnClickListener(this);
        ll_operation_thesame = (LinearLayout) findViewById(R.id.ll_operation_thesame);
        rb_comment_sametime = (CheckBox) findViewById(R.id.rb_comment_sametime);
        tv_extra_feature_desc = (TextView) findViewById(R.id.tv_extra_feature_desc);
        ll_operation_thesame.setOnClickListener(this);
        insertPicView = (ImageView) findViewById(R.id.iv_insertpic);
        ibInsertPic = (ImageButton) findViewById(R.id.ib_insert_pic);
        if (mode == 1 || mode == 2 || mode == 3 || mode == 5 || mode == 6) {
            ibInsertPic.setVisibility(View.GONE);
        }
        if (mode == 0) {
            String strPic = Helper.getString(this, "praImage", "");
            strContent = Helper.getString(this, "content", strContent);
            if (!TextUtils.isEmpty(strPic)) {
                bitmap = BitmapFactory.decodeFile(fPic.getPath());
                insertPicView.setImageBitmap(bitmap);
                insertPicView.setVisibility(View.VISIBLE);
                Helper.delString(this, "praImage");
            }
            Helper.delString(this, "content");
        }
        if (mode == 3 || mode == 6) {
            strContent = Helper.getString(this, "message", strContent);
            Helper.delString(this, "message");
        }
        ibInsertTopic = (ImageButton) findViewById(R.id.ib_insert_topic);
        ibInsertAt = (ImageButton) findViewById(R.id.ib_insert_at);
        ibInsertFace = (ImageButton) findViewById(R.id.ib_face_keyboard);
        ibInsertFace.setOnClickListener(this);
        insertPicView.setOnClickListener(this);
        ibInsertTopic.setOnClickListener(this);
        ibInsertAt.setOnClickListener(this);
        ibInsertPic.setOnClickListener(this);
        if (!TextUtils.isEmpty(getIntent().getAction()) && getIntent().getAction().equals(Intent.ACTION_SEND)) {
            loadShareImage();
        }
        if (mode == 2) {
            mEt.requestFocus();
            ll_operation_thesame.setVisibility(View.VISIBLE);
            tv_extra_feature_desc.setText(R.string.forward_add_comment);
        } else if (mode == 1 || mode == 5) {
            ll_operation_thesame.setVisibility(View.VISIBLE);
            tv_extra_feature_desc.setText(R.string.comment_add_forward);
            rb_comment_sametime.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        mode = 2;
                    } else {
                        mode = 1;
                    }
                }
            });
        } else if (mode == 3 || mode == 6) {
            tv_text_limit.setText("300");
            allword = 300;
        }
        mEt.setText(strContent);
        TopicUtil.highlightContent(EditActivity.this, mEt.getText(), false);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yingnet.zhiweibo.BaseActivity#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        if (v == llimte) {
            new AlertDialog.Builder(EditActivity.this).setTitle(R.string.attention_all)
                    .setMessage(R.string.delete_text)
                    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                            mEt.setText("");
                        }
                    }).setNegativeButton(R.string.cancel, null).create().show();
        } else if (v == ibInsertPic) {
            showDialog(1001);
        } else if (v == ibInsertAt) {
            int j = -1;
            String str1 = "@XXX:";
            int k = mEt.getSelectionStart();
            int m = mEt.getSelectionEnd();
            String str2 = mEt.getText().toString();
            if ((k == j) || (m == j) || (k > m)) {
                mEt.append(str1);
                int n = mEt.getText().toString().length();
                int i1 = str1.length();
                int i2 = n - i1 + 1;
                int i3 = n - 1;
                mEt.setSelection(i2, i3);
            } else {
                String str3 = str2.substring(0, k);
                String str4 = str2.substring(m);
                mEt.setText(str3 + str1 + str4);
                int i4 = k + 1;
                int i5 = str1.length() + k - 1;
                Selection.setSelection(mEt.getText(), i4, i5);
            }
            TopicUtil.highlightContent(EditActivity.this, mEt.getText(), false);
        } else if (v == ibInsertTopic) {
            int j = -1;
            String s = "#主题#";
            int k = mEt.getSelectionStart();
            int l = mEt.getSelectionEnd();
            String s1 = mEt.getText().toString();
            if (k == j || l == j || k > l) {
                mEt.append(s);
                int i1 = mEt.getText().toString().length();
                int j1 = s.length();
                int k1 = i1 - j1 + 1;
                int l1 = i1 - 1;
                mEt.setSelection(k1, l1);
            } else {
                String s2 = s1.substring(0, k);
                String s3 = s1.substring(l);
                mEt.setText(s2 + s + s3);
                int i2 = k + 1;
                int j2 = (s.length() + k) - 1;
                Selection.setSelection(mEt.getText(), i2, j2);
            }
            TopicUtil.highlightContent(EditActivity.this, mEt.getText(), false);
        } else if (v == insertPicView) {
            insertPicView.setImageBitmap(null);
            insertPicView.setVisibility(View.GONE);
        } else if (v == ibInsertFace) {
            mFaceClickHealper.onClick(v);
        } else if (v == mEt) {
            if (mFaceClickHealper.isFaceDiaplay()) {
                mFaceClickHealper.onClick(v);
            }
        } else if (v == ll_operation_thesame) {
            rb_comment_sametime.performClick();
        } else {
            super.onClick(v);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
     */
    @Override
    public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent) {
        if (paramInt == 4) {
            if (mFaceClickHealper.onFinish()) {
                return true;
            }
        }
        return super.onKeyDown(paramInt, paramKeyEvent);

    }

    /**
     * Sets the emotion view visibility.
     * 
     * @param paramBoolean
     *            the new emotion view visibility
     */
    void setEmotionViewVisibility(boolean paramBoolean) {
        if (mEmotionView != null) {
            mEmotionView.setVisibility(paramBoolean ? View.VISIBLE : View.GONE);
        }
    }

    /**
     * Display face image src.
     */
    void displayFaceImageSrc() {
        if (ibInsertFace != null) {
            ibInsertFace.setImageResource(R.drawable.btn_insert_face);
        }
    }

    /**
     * Display keyboard image src.
     */
    void displayKeyboardImageSrc() {
        if (ibInsertFace != null) {
            ibInsertFace.setImageResource(R.drawable.btn_insert_keyboard);
        }
    }

    /**
     * Sets the input method visibility.
     * 
     * @param paramBoolean
     *            the param boolean
     * @return true, if successful
     */
    boolean setInputMethodVisibility(boolean paramBoolean) {
        if ((mInputMethodManager != null) && (mEt != null)) {
            if (!paramBoolean) {
                if (mInputMethodManager.isActive(mEt)) {
                    mInputMethodManager.hideSoftInputFromWindow(mEt.getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                }
            } else {
                mInputMethodManager.showSoftInput(mEt, 0);
            }
        }
        return false;
    }

    /**
     * Load share image.
     */
    protected void loadShareImage() {
        Uri localUri = (Uri) getIntent().getParcelableExtra("android.intent.extra.STREAM");
        if (localUri != null) {
            try {
                BitmapFactory.Options options = new BitmapFactory.Options();
                bitmap = BitmapFactory.decodeStream(this.getContentResolver().openInputStream(localUri), null, options);
                int width = bitmap.getWidth();
                int height = bitmap.getHeight();
                int newWidth = 480;
                int newHeight = 640;
                if (width > height) {
                    newWidth = 640;
                    newHeight = 480;
                }
                float scaleWidth = ((float) newWidth) / width;
                float scaleHeight = ((float) newHeight) / height;
                float minscale = Math.min(scaleWidth, scaleHeight);
                Matrix matrix = new Matrix();
                matrix.postScale(minscale, minscale);
                bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
            } catch (FileNotFoundException e) {
                Logger.d(e);
            }
            insertPicView.setImageBitmap(bitmap);
            insertPicView.setVisibility(0);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onCreateDialog(int)
     */
    @Override
    protected Dialog onCreateDialog(int paramInt) {
        if (paramInt == 1001) {
            CharSequence[] arrayOfCharSequence1 = new CharSequence[2];
            arrayOfCharSequence1[0] = getString(R.string.menu_camera);
            arrayOfCharSequence1[1] = getString(R.string.menu_gallery);
            return new AlertDialog.Builder(this).setTitle(R.string.menu_settings)
                    .setItems(arrayOfCharSequence1, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                            switch (paramInt) {
                            default:
                            case 0:
                                startToCameraActivity();
                                break;
                            case 1:
                                startToMediaActivity();
                                break;
                            }
                        }
                    }).create();
        } else if (paramInt == 1007) {
            return new AlertDialog.Builder(this).setTitle(R.string.attention_all)
                    .setMessage(R.string.delete_draft_or_not)
                    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                            if (insertPicView.getVisibility() != View.GONE) {
                                try {
                                    FileOutputStream fOut = new FileOutputStream(fPic);
                                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
                                    fOut.flush();
                                    fOut.close();
                                } catch (FileNotFoundException e) {
                                    Logger.d(e);
                                } catch (IOException e) {
                                    Logger.d(e);
                                }
                                Helper.saveString(EditActivity.this, "praImage", "1");
                            }
                            Helper.saveString(EditActivity.this, "content", mEt.getText().toString());
                            finish();
                        }
                    }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                            finish();
                        }
                    }).create();
        } else if (paramInt == 1008) {
            return new AlertDialog.Builder(this).setTitle(R.string.attention_all)
                    .setMessage(R.string.message_draft_or_not)
                    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                            Helper.saveString(EditActivity.this, "message", mEt.getText().toString());
                            finish();
                        }
                    }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                            finish();
                        }
                    }).create();
        }
        return null;

    }

    /**
     * Start to camera activity.
     */
    private void startToCameraActivity() {
        if (Environment.getExternalStorageState().equals("mounted")) {
            Intent localIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File localFile1 = Environment.getExternalStorageDirectory();
            String str3 = "tmp_bmp.jpg";
            sdcardTempFile = new File(localFile1, str3);
            localIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(sdcardTempFile));
            startActivityForResult(localIntent, 0);
        } else {
            Toast.makeText(this, R.string.pls_insert_sdcard, 0).show();
        }
    }

    /**
     * Start to media activity.
     */
    private void startToMediaActivity() {
        Uri localUri = MediaStore.Images.Media.INTERNAL_CONTENT_URI;
        Intent localIntent = new Intent("android.intent.action.PICK", localUri);
        startActivityForResult(localIntent, 100);
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onActivityResult(int, int,
     * android.content.Intent)
     */
    @Override
    protected void onActivityResult(int paramInt1, int paramInt2, Intent paramIntent) {
        if ((paramInt1 == 100) || (paramInt1 == 0)) {
            if (paramInt2 == -1) {
                if (paramInt1 == 0 && sdcardTempFile.exists()) {
                    localUri = Uri.fromFile(sdcardTempFile);
                } else if (100 == paramInt1) {
                    localUri = paramIntent.getData();
                }
                BitmapFactory.Options options = new BitmapFactory.Options();
                try {
                    bitmap = BitmapFactory.decodeStream(this.getContentResolver().openInputStream(localUri), null,
                            options);
                    int width = bitmap.getWidth();
                    int height = bitmap.getHeight();
                    int newWidth = 480;
                    int newHeight = 640;
                    if (width > height) {
                        newWidth = 640;
                        newHeight = 480;
                    }
                    float scaleWidth = ((float) newWidth) / width;
                    float scaleHeight = ((float) newHeight) / height;
                    float minscale = Math.min(scaleWidth, scaleHeight);
                    Matrix matrix = new Matrix();
                    matrix.postScale(minscale, minscale);
                    bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
                } catch (FileNotFoundException e) {
                    Logger.d(e);
                }
                insertPicView.setImageBitmap(bitmap);
                insertPicView.setVisibility(0);
            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yingnet.zhiweibo.BaseActivity#handleTitleBarEvent(int)
     */
    @Override
    protected void handleTitleBarEvent(int paramInt) {
        switch (paramInt) {
        case 0:
            if (Integer.parseInt(tv_text_limit.getText().toString()) <= 0) {
                Toast.makeText(EditActivity.this, "内容不能为空", Toast.LENGTH_SHORT).show();
            } else {
                editTask = new EditTask();
                editTask.execute(new Object());
            }
            break;
        case 1:
            if (mode == 0) {
                if (!TextUtils.isEmpty(mEt.getText().toString()) || insertPicView.getVisibility() != View.GONE) {
                    showDialog(1007);
                } else {
                    finish();
                }
            } else if (mode == 3 || mode == 6) {
                if (!TextUtils.isEmpty(mEt.getText().toString())) {
                    showDialog(1008);
                } else {
                    finish();
                }
            } else {
                finish();
            }
            break;
        }
    }

    /**
     * Call back.
     */
    private void callBack() {
        if (TextUtils.isEmpty(topic.getTid())) {
            Toast.makeText(this, "发送失败", Toast.LENGTH_SHORT).show();
        } else {
            finish();
        }
    }

    /**
     * The Class EditTask.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class EditTask extends AsyncTask {

        /** The pd. */
        private ProgressDialog pd;

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected Object doInBackground(Object... params) {
            String strUrl;
            switch (mode) {
            case 0:
            case 4:
                if (insertPicView.getVisibility() == View.GONE) {
                    strUrl = Constant.UPDATE + "?contents=" + URLEncoder.encode(mEt.getText().toString());
                    topic = HttpUtil.getTopic(EditActivity.this, strUrl);
                } else {
                    topic = HttpUtil.postTopic(EditActivity.this, Constant.UPLOAD, mEt.getText().toString(), bitmap);
                }
                break;
            case 1:
                strUrl = Constant.COMMENT + "?contents=" + URLEncoder.encode(mEt.getText().toString()) + "&tid="
                        + strTopicId;
                topic = HttpUtil.getComment(EditActivity.this, strUrl).getTopic();
                break;
            case 2:
                strUrl = Constant.REPOST + "?contents=" + URLEncoder.encode(mEt.getText().toString()) + "&tid="
                        + strTopicId;
                if (rb_comment_sametime.isChecked()) {
                    strUrl += "&otid=" + strTopicId;
                }
                topic = HttpUtil.getTopic(EditActivity.this, strUrl);
                break;
            case 3:
                strUrl = Constant.SEND + "?contents=" + URLEncoder.encode(mEt.getText().toString()) + "&uid=" + uid;
                HttpUtil.exeUrl(EditActivity.this, strUrl);
                topic = new Topic();
                topic.setTid("forward");
                break;
            case 5:
                strUrl = Constant.REPLY + "?contents=" + URLEncoder.encode(mEt.getText().toString()) + "&cid=" + cid
                        + "&tid=" + strTopicId;
                HttpUtil.getTopic(EditActivity.this, strUrl);
                topic = new Topic();
                topic.setTid("forward");
                break;

            }
            return topic;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Object result) {
            callBack();
            if (pd != null) {
                pd.dismiss();
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPreExecute()
         */
        @Override
        protected void onPreExecute() {
            pd = new ProgressDialog(EditActivity.this);
            pd.setMessage("发送中……");
            pd.show();
        }

    }

    /*
     * (non-Javadoc)
     * 
     * @see android.text.TextWatcher#beforeTextChanged(java.lang.CharSequence,
     * int, int, int)
     */
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.text.TextWatcher#onTextChanged(java.lang.CharSequence, int,
     * int, int)
     */
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    /*
     * (non-Javadoc)
     * 
     * @see android.text.TextWatcher#afterTextChanged(android.text.Editable)
     */
    @Override
    public void afterTextChanged(Editable s) {
        int sum = 0;
        int k = s.toString().length();
        for (int j = 0; j < k; j++) {
            int m = (char) s.toString().codePointAt(j);
            int n = 255;
            if (m > n) {
                sum += 2;
            } else {
                sum += 1;
            }
        }
        sum = (int) Math.ceil(sum / 2);
        tv_text_limit.setText((allword - sum) + "");
    }
}
